package com.uzai.console.service.cdkeys.impl;

import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.taobao.api.internal.toplink.embedded.websocket.util.StringUtil;
import com.uzai.common.entity.WechatUser;
import com.uzai.common.exception.BusinessException;
import com.uzai.common.result.ResponseCode;
import com.uzai.console.common.utils.Tools;
import com.uzai.console.dto.cdkeys.CdkeysOldCardExchangeDto;
import com.uzai.console.entity.Cdkeys;
import com.uzai.console.jsonvo.cdkeys.CardExchangeResult;
import com.uzai.console.mapper.CdkeysMapper;
import com.uzai.console.mapper.CmsActivityDetailMapper;
import com.uzai.console.mapper.CmsActivityMapper;
import com.uzai.console.mapper.CmsMerTbGoodsMapper;
import com.uzai.console.para.CdkeysOldCardExchangeRemoteApiPara;
import com.uzai.console.service.cdkeys.CdkeysService;
import com.uzai.console.service.cms.CmsTbGoodsService;
import com.uzai.console.service.remoteapi.CdkeysRemoteApiService;
import com.uzai.console.vo.cdkeys.CdkeysOldCardExchangeVo;
import com.uzai.console.vo.remoteapi.cdkey.CdkeyConvertVo;
import com.uzai.console.vo.user.wechatuser.WechatUserPageVo;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * @author liuqi
 * @date 2021年08月23日 20:25
 */
@Service
public class CdkeysServiceImpl implements CdkeysService {

    Logger logger = LoggerFactory.getLogger(CdkeysServiceImpl.class);

    @Autowired
    private CdkeysRemoteApiService cdkeysRemoteApiService;
    @Autowired
    private CdkeysMapper cdkeysMapper;

    /**
     * 2.0旧卡更换3.0新卡
     * @author liuqi
     * @date 2021/8/13 11:38
     * @return CmsActivityInfoVo
     */
    public CdkeysOldCardExchangeVo oldCardExchange(CdkeysOldCardExchangeDto cdkeysOldCardExchangeDto){

        if(cdkeysOldCardExchangeDto.getCards() == null || cdkeysOldCardExchangeDto.getCards().size() == 0){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "请输入卡号");
        }

        if(cdkeysOldCardExchangeDto.getCards().size() > 50){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "卡号数量最多支持50张");
        }

        //返回对象
        CdkeysOldCardExchangeVo cdkeysOldCardExchangeVo = new CdkeysOldCardExchangeVo();
        //成功卡号
        List<CardExchangeResult> cardExchangeResultList = new ArrayList<>();

        //兑换码
        String convertCode = IdWorker.getIdStr();
        CdkeysOldCardExchangeRemoteApiPara cdkeysOldCardExchangeRemoteApiPara = new CdkeysOldCardExchangeRemoteApiPara();
        cdkeysOldCardExchangeRemoteApiPara.setCards(cdkeysOldCardExchangeDto.getCards());
        cdkeysOldCardExchangeRemoteApiPara.setKey("12ea6e2f0dc40a850f5a2f2ce8abadff");
        cdkeysOldCardExchangeRemoteApiPara.setConvert_code(convertCode);
        List<CdkeyConvertVo> cdkeyConvertVoList = cdkeysRemoteApiService.convert(cdkeysOldCardExchangeRemoteApiPara);

        if(cdkeyConvertVoList != null && cdkeyConvertVoList.size() > 0){
            for(CdkeyConvertVo cdkeyConvertVo : cdkeyConvertVoList){
                CardExchangeResult cardExchangeResult = new CardExchangeResult();
                BeanUtils.copyProperties(cdkeyConvertVo, cardExchangeResult);
                cardExchangeResultList.add(cardExchangeResult);
                if(Tools.getInteger(cardExchangeResult.getUsestate()).intValue() == 1){ //只有1才可以兑换
                    //只有类型为1-月卡和12-年卡才能兑换
                    if(Tools.getInteger(cardExchangeResult.getAuthmonth()).intValue() == 1 || Tools.getInteger(cardExchangeResult.getAuthmonth()).intValue() == 12){
                        Integer type = 0;
                        if(cardExchangeResult.getAuthmonth().intValue() == 1){
                            type = 1;
                        }else if(cardExchangeResult.getAuthmonth().intValue() == 12){
                            type = 2;
                        }
                        if(type.intValue() == 1 || type.intValue() == 2){
                            Cdkeys cdkeys = cdkeysMapper.selectCardForOldCardExchange(type);
                            if(cdkeys != null && cdkeys.getStatus().intValue() == 0  && StringUtils.isBlank(cdkeys.getOldCard())){
                                //先修改旧卡号，兑换码，出库状态
                                cdkeys.setOldCard(cardExchangeResult.getCard());
                                cdkeys.setConvertCode(convertCode);
                                cdkeys.setStatus(1);
                                int j = cdkeysMapper.updateById(cdkeys);
                                if(j == 1){
                                    //返回给对象
                                    cardExchangeResult.setNewCard(cdkeys.getCard());
                                }
                            }
                        }
                    }
                }else if(Tools.getInteger(cardExchangeResult.getUsestate()).intValue() == 3){ //已兑换，需要将兑换的新卡返回
                    Cdkeys cdkeys = cdkeysMapper.selectByOldCard(cardExchangeResult.getCard());
                    cardExchangeResult.setNewCard(cdkeys.getCard());
                }
            }
        }
        cdkeysOldCardExchangeVo.setCardExchangeResultList(cardExchangeResultList);
        return cdkeysOldCardExchangeVo;
    }




}
